package com.controller.hotel;

import com.biz.BillBiz;
import com.wechat.mina.*;
import com.biz.BillRefundBiz;
import com.biz.HotelReservationBiz;
import com.controller.backend.BaseController;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.Bill;
import com.entity.model.BillRefund;
import com.entity.model.HotelAdmin;
import com.entity.model.HotelReservation;
import com.entity.vo.BillListVO;
import com.github.pagehelper.PageInfo;
import com.sms.SmsUtil;
import com.util.CalendarUtil;
import com.util.SystemConstant;
import com.util.webpage.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by jc on 2019/11/16.
 */
@Controller
@RequestMapping("/hotel/security/bill")
public class HotelBillController extends BaseController<Bill> {
    @Autowired
    private BillBiz billBiz;

    @Autowired
    private BillRefundBiz billRefundBiz;

    @Autowired
    private HotelReservationBiz hotelReservationBiz;

    //订单列表
    @RequestMapping(value = "index")
    public String index() throws Exception {
        return "/hotel/security/bill/list";
    }

    @RequestMapping(value = "listAlready")
    public String listAlready() throws Exception {
        return "/hotel/security/bill/listAlready";
    }

    @RequestMapping(value = "listCancel")
    public String listCancel() throws Exception {
        return "/hotel/security/bill/listCancel";
    }

    @RequestMapping(value = "listComplete")
    public String listComplete() throws Exception {
        return "/hotel/security/bill/listComplete";
    }

    @RequestMapping(value = "listTobe")
    public String listTobe() throws Exception {
        return "/hotel/security/bill/listTobe";
    }

    @RequestMapping(value = "listRefund")
    public String listRefund() throws Exception {
        return "/hotel/security/bill/listRefund";
    }

    //订单详情
    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "/hotel/security/bill/form";
    }


    //获取订单列表详情
    @RequestMapping(value = "billList")
    @ResponseBody
    public Result billList(HttpServletRequest request) {
        HotelAdmin hotelAdmin = (HotelAdmin) request.getSession().getAttribute(SystemConstant.SESSION_HOTEL_ADMIN);
        PageData cond = new PageData(request);
        cond.put("hotelId", hotelAdmin.getHotelId());
        List<Bill> list = billBiz.billList(cond);
        PageInfo<Bill> pageInfo = new PageInfo<Bill>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }


    @RequestMapping(value = "updateCheck")
    @ResponseBody
    public Result updateCheck(Integer id, Integer status) throws Exception {

        BillListVO list = billBiz.getBillDetail(id);

        if (status == 2) {
            Bill bill = billBiz.findById(id);

            HotelReservation hotelReservation = hotelReservationBiz.findBy("billId", id);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String checkInTime = formatter.format(hotelReservation.getReserveStartTime());
            if (CalendarUtil.getToday().getTime() < CalendarUtil.strToDateYmd(checkInTime).getTime()) {
                return ResultGenerator.genFailResult("入住当日14点以后才能办理入住");
            }

            bill.setStatus(3);
            billBiz.update(bill);

            SmsUtil.checkInMsg(list.getAccount(), list.getHotelName(), list.getHotelTelNo());
        } else if (status == 3) {

            Bill bill = billBiz.findById(id);
            bill.setStatus(status);
            bill.setCheckOutTime(new Date());

            BillRefund billRefund = new BillRefund();
            billRefund.setBillNo(bill.getBillNo());
            billRefund.setBillFee(bill.getBillFee());
            billRefund.setBillId(bill.getId());
            billRefund.setHomeId(bill.getHomeId());
            billRefund.setHotelId(bill.getHotelId());
            billRefund.setCustomerId(bill.getCustomerId());
            billRefund.setCheckOutTime(new Date());


            HotelReservation hotelReservation = hotelReservationBiz.findBy("billId", bill.getId());
            if (hotelReservation != null) {
                hotelReservation.setStatus(2);
                hotelReservationBiz.update(hotelReservation);

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                String checkOutTimeSt = formatter.format(hotelReservation.getReserveStartTime()) + " 00:00:00";
                if (hotelReservation.getReserveEndTime().getTime() >= CalendarUtil.getNow().getTime() && CalendarUtil.getNow().getTime() >= CalendarUtil.strToDate(checkOutTimeSt).getTime()) {
                    return billRefundBiz.refund(bill.getId(), billRefund.getId());
                } else if(hotelReservation.getReserveEndTime().getTime() <= CalendarUtil.getNow().getTime()){
                    bill.setIsDefault(1);
                    billRefund.setIsDefault(1);
                    billRefund.setRemark("超时离店");
                }  else if(CalendarUtil.getNow().getTime() <= CalendarUtil.strToDate(checkOutTimeSt).getTime()){
                    bill.setIsDefault(1);
                    billRefund.setIsDefault(1);
                    billRefund.setRemark("提前离店");
                }
            }

            billBiz.update(bill);
            billRefundBiz.save(billRefund);
            SmsUtil.checkOutMsg(list.getAccount(), list.getHotelName(), list.getHotelTelNo());
        }
        return ResultGenerator.genSuccessResult();
    }
}
